var class_num=location.search;
class_num=class_num.split('?')[1];
$('header h1').append(class_num);
$('#href_list').attr('href','details_stu.html?'+class_num);
$('#href_course').attr('href','details_course.html?'+class_num);
$('#homework').attr('href','homework.html?'+class_num);
$('#href_file').attr('href','details_file.html?'+class_num);
if (islogin()) {;
    $.post({
        url:'../../requests/teacher/select_homework.php',
        data:{'class':class_num},
        success:function (data) {
            if (data == "") {
                $('ul').append('该班级还没有作业');
            }
            else {
                for (var i = 0; i < data.length; i++) {
                    if (!checktime(data[i].deadline)) {
                        var dom="<li class='item-content item-link'>" +
                            "<div class='item-inner'>"+
                            "<div class='item-title not-overtime'>"+data[i].title+"</div>"+
                            "<div class='item-after' id='"+data[i].Id+"'>查看</div>"+
                            "</div>"+
                            "</li>";
                    }
                    else var dom="<li class='item-content item-link'>" +
                                "<div class='item-inner'>"+
                                    "<div class='item-title'>"+data[i].title+"</div>"+
                                    "<div class='item-after' id='"+data[i].Id+"'>查看</div>"+
                                "</div>"+
                            "</li>";
                    $('ul').append(dom);
                }
            }
        }
    });

    $('ul').on('click','.item-after',function () {
        window.location.href='detail_homework.html?'+$(this).attr('id');
    });

    $('#op').click(function () {
        var button1=[
            {
                text:'添加',
                onClick:function () {
                    window.location.href='add_homework.html?'+class_num;
                }
            }
        ];
        var button2=[{
            text:'取消',
        }];
        var groups=[button1,button2];
        $$.actions(groups);
    });
}
else window.location.href='../../index.html';

//检查作业是否过期
function checktime(deadline) {
    if (deadline == "0000-00-00 00:00:00") {
        return false;
    }
    else {
        var mydate = new Date();
        var time = format(mydate, '-');
        var check = false;
        time = new Date(time.replace(/\-/g, '\/'));
        deadline = new Date(deadline.replace(/\-/g, '\/'));
        if (time >= deadline) {
            check = true;
        }
        else check = false;
        return check;
    }

}
//格式化时间
function format(Date,str){
    var obj = {
        Y: Date.getFullYear(),
        M: Date.getMonth() + 1,
        D: Date.getDate(),
        H: Date.getHours(),
        Mi: Date.getMinutes(),
        S: Date.getSeconds()
    }
    // 拼接时间 hh:mm:ss
    var time = ' ' +supplement(obj.H) + ':' + supplement(obj.Mi) + ':' + supplement(obj.S);
    // yyyy-mm-dd
    if(str.indexOf('-') > -1){
        return obj.Y + '-' + supplement(obj.M) + '-' + supplement(obj.D) + time;
    }
    // yyyy/mm/dd
    if(str.indexOf('/') > -1){
        return obj.Y + '/' + supplement(obj.M) + '/' + supplement(obj.D) + time;
    }
}
// 位数不足两位补全0
function supplement(nn){
    return nn = nn < 10 ? '0' + nn : nn;
}
